package cn.school.controller.attendance;

import cn.school.attendance.classes.entity.ClassesAttendanceDo;
import cn.school.attendance.classes.service.ClassesAttendanceInfoService;
import cn.school.attendance.classes.vo.ClassesAttendanceInfoPageParam;
import cn.school.attendance.classes.vo.ClassesAttendanceVo;
import cn.school.attendance.classes.vo.LeaveAndNotArrivedStudentInfoVo;
import cn.school.common.api.CommonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 班主任查看班级考勤功能
 * @Author tzw
 * @Date 2024-03-22
 */
@RestController
@RequestMapping("/attendance/classAttendance")
public class ClassesAttendanceInfoController {
    @Resource
    private ClassesAttendanceInfoService classesAttendanceInfoService;


    /**
     * 查看班级在校学生列表
     * @param classesAttendanceInfoPageParam
     * @return
     */
    @ApiOperation("查询班级考勤列表")
    @PostMapping("/findPageList")
    public CommonResult<ClassesAttendanceVo> getClassAttendInfoList(@RequestBody ClassesAttendanceInfoPageParam classesAttendanceInfoPageParam) {
        if (classesAttendanceInfoPageParam.getPageNum() == 1){
            classesAttendanceInfoService.checkStudentTrajectoryUpdate(classesAttendanceInfoPageParam);
        }
        ClassesAttendanceVo classesAttendanceList = classesAttendanceInfoService.getStudentAttendancePageList(classesAttendanceInfoPageParam);
        return CommonResult.success(classesAttendanceList);
    }

    @ApiOperation("更新学生考勤信息")
    @PostMapping("/updateAttendanceInfoStatus")
    public CommonResult updateAttendanceInfoStatus(@RequestBody ClassesAttendanceDo classesAttendanceDo) {
        classesAttendanceInfoService.updateAttendanceInfoStatus(classesAttendanceDo);
        return CommonResult.success();
    }

    /**
     * 查看班级请假学生列表
     * @param classesAttendanceInfoPageParam
     * @return
     */
    @ApiOperation("获取班级请假学生列表")
    @PostMapping("/getClassLeaveStudent")
    public CommonResult getClassLeaveStudent(@RequestBody ClassesAttendanceInfoPageParam classesAttendanceInfoPageParam) {
        List<LeaveAndNotArrivedStudentInfoVo> leaveStudentInfoList =  classesAttendanceInfoService.getLeaveStudentInfoList(classesAttendanceInfoPageParam);
        List<LeaveAndNotArrivedStudentInfoVo> distinctStudentInfoList = leaveStudentInfoList.stream()
                // 根据studentId属性对列表元素进行分组
                .collect(Collectors.groupingBy(LeaveAndNotArrivedStudentInfoVo::getStudentId))
                // 取出每个分组中的第一条数据（即具有相同studentId的元素中保留一个）
                .values().stream()
                .map(group -> group.iterator().next())
                .collect(Collectors.toList());
        return CommonResult.success(distinctStudentInfoList);
    }
}
